import json

from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from apps.badminton_school.models import BookSite,Reservation,schoolSite,BallTogether,spc_Activity
import time

import datetime

from apps.users.views import LoginRequiredJsonMixin


class getBadminSiteInfo(View):
    def get(self,request,date,site,time):
        site_small=schoolSite.objects.get(id=site)
        # 注意：date必须是2022-03-05这个形式
        time=int(time)
        site_info=BookSite.objects.filter(date=date,site=site_small,time_slot=time,status=0)
        info=[]

        count=site_info.count()
        # BookSite.objects.filter(site=1).count()
        for item in site_info:
            info.append({
                'areaname':item.site.areaname,
                'price':item.site.price,
                'info_message':item.site.info_message,
                'status':item.status,
                'beizhu':item.info,
                'time_zone':item.time_slot,
                # ''
            })
        return JsonResponse({'code':0,'info':info,'count':count})


class getBadminSiteInfoDate(View):
    def get(self,request,date):
        site_info=BookSite.objects.filter(date=date,status=0)
        info=[]
        count=site_info.count()
        for item in site_info:
            info.append({
                'areaname':item.site.areaname,
                'price':item.site.price,
                'info_message':item.site.info_message,
                'status':item.status,
                'beizhu':item.info,
                'time_zone':item.time_slot,
                # ''
            })
        return JsonResponse({'code':0,'info':info,'count':count})

class getBadminSiteInfoDateTime(View):
    def get(self,request,date,time):
        time=int(time)
        site_info=BookSite.objects.filter(date=date,time_slot=time,status=0)
        info=[]
        count=site_info.count()
        for item in site_info:
            info.append({
                'areaname':item.site.areaname,
                'price':item.site.price,
                'info_message':item.site.info_message,
                'status':item.status,
                'beizhu':item.info,
                'time_zone':item.time_slot,

                # ''
            })
        return JsonResponse({'code':0,'info':info,'count':count})
class getBookInfo(View):
    def get(self,request):
        datetime_now = time.strftime("%Y-%m-%d")
        book_count=len(list(BookSite.objects.filter(site=1,date=datetime_now)))
        book0 = list(BookSite.objects.filter(time_slot=0, date=datetime_now))
        if len(book0)==0:
            return JsonResponse({'code':200,'errmsg':'今日预定信息未更新','info':[]})
        book_3 = list(BookSite.objects.filter(time_slot=-3, date=datetime_now))
        book_2 = list(BookSite.objects.filter(time_slot=-2, date=datetime_now))
        book_1=list(BookSite.objects.filter(time_slot=-1,date=datetime_now))
        book1=list(BookSite.objects.filter(time_slot=1,date=datetime_now))
        book2=list(BookSite.objects.filter(time_slot=2,date=datetime_now))
        book3=list(BookSite.objects.filter(time_slot=3,date=datetime_now))
        book4=list(BookSite.objects.filter(time_slot=4,date=datetime_now))
        abc=[book_3,book_2,book_1,book0,book1,book2,book3,book4]
        info=[]
        for i in range(0,book_count):
            info.append({
                "time":book1[0].TIME_CHOICES[i][1],
                'status1': {
                    'id': abc[i][0].id,
                    'status': abc[i][0].status,
                },
                'status2': {
                    'id': abc[i][1].id,
                    'status': abc[i][1].status,
                },
                'status3': {
                    'id': abc[i][2].id,
                    'status': abc[i][2].status,
                },
                'status4': {
                    'id': abc[i][3].id,
                    'status': abc[i][3].status,
                },
                'status5': {
                    'id': abc[i][4].id,
                    'status': abc[i][4].status,
                },
                'status6': {
                    'id': abc[i][5].id,
                    'status': abc[i][5].status,
                },
                'status7': {
                    'id': abc[i][6].id,
                    'status': abc[i][6].status,
                },
                'status8': {
                    'id': abc[i][7].id,
                    'status': abc[i][7].status,
                },
                'status9': {
                    'id': abc[i][8].id,
                    'status': abc[i][8].status,
                },
                'status10': {
                    'id': abc[i][9].id,
                    'status': abc[i][9].status,
                },

            })
        return JsonResponse({'code':0,'info':info})


        # for item in site_info:
        #     info.append({
        #         'areaname':item.site.areaname,
        #         'price':item.site.price,
        #         'info_message':item.site.info_message,
        #         'status':item.status,
        #         'beizhu':item.info,
        #         'time_zone':item.time_slot,
        #
        #         # ''
        #     })
        # return JsonResponse({'code':0,'info':info,'count':count})
class updateInfo(View):
    def get(self,request):
        datetime_now=time.strftime("%Y-%m-%d")
        BookSite.objects.all().update(date=datetime_now,status=0)
        return JsonResponse({'code':0,'errmsg':datetime_now+'更新信息完成'})

class getTryInfo(View):
    def get(self,request):
        roleId=request.GET.get('id')
        info=BookSite.objects.get(id=roleId)
        return JsonResponse({'code':0,'errmsg':'获取成功','id':roleId,'time':info.TIME_CHOICES[info.time_slot+3][1],'name':info.site.areaname})

class addbookinfo(LoginRequiredJsonMixin,View):
    def post(self,request):
        datetime_now = time.strftime("%Y-%m-%d")
        data = json.loads(request.body.decode())
        user = request.user

        try:
            site_id = data.get('id')
        except:
            return JsonResponse({'code':200,'errmsg':'预定失败，请选择预约场地'})
        status=BookSite.objects.get(id=site_id).status
        count=Reservation.objects.filter(pre_date_day=datetime_now,user=user).count()
        if count==0:
            if status==0:
                Reservation.objects.create(pre_status=1,site_ID_id=site_id,user=user)
                BookSite.objects.filter(id=site_id).update(status=2)
                return JsonResponse({'code':0,'errmsg':'预定成功'})
            else:
                return JsonResponse({'code':200,'errmsg':'预约失败，该场地已被占用'})
        else:
            return JsonResponse({'code':200,'errmsg':'预定失败，今日已经预定场地，不可重复预定'})



class TestTime(View):
    def post(selfs,request):
        data = json.loads(request.body.decode())
        user = request.user
        time=data.get('time')
        abc=BallTogether.objects.filter(id=4).update(begin_time=time)
        return JsonResponse({'code':0,'errmsg':'时间测试成功'})

class Activity(LoginRequiredJsonMixin,View):
    def post(self,request):
        data=json.loads(request.body.decode())
        user=request.user
        name=data.get('name')
        begin_time=data.get('begin_time')
        deadline_time=data.get('deadline_time')
        site=data.get('site')
        max_people=data.get('max_people')
        item_index=data.get('item_index')
        introduce=data.get('introduce')
        hope_level=data.get('hope_level')
        contact=data.get('phone')
        sex_index=data.get('index_sex')
        money=data.get('money')

        if not all([name,begin_time,deadline_time,site,max_people,item_index,contact,sex_index]):
            return JsonResponse({'code': 400, 'errmsg': '信息不全,请检查是否信息为空'})
        abc=BallTogether.objects.create(name=name,holder=user,deadline_time=deadline_time,begin_time=begin_time,
                                    site=site,max_people=max_people,event=item_index,Introduce=introduce,
                                    hope_lever=hope_level,contact=contact,pass_choice=1,limit_sex=sex_index,money=money)
        return JsonResponse({'code':0,'errmsg':'新增活动成功'})
class Act_Info(View):
    def get(self,request):
        ball_info=BallTogether.objects.filter(pass_choice=1).order_by('-id')
        act_info=[]
        for item in ball_info:
            act_info.append({
                'id':item.id,
                'holder_name':item.holder.nickname,
                'holder_img_header': str(item.holder.img_header),
                'hold_time':item.begin_time,
                'site':item.site,
                'name':item.name,
                'max_people':item.max_people,
                'sign_num':item.sign_num,
                'end_time':item.deadline_time,
                'item':item.EVENT_CHOICE[item.event-1][1]

            })
        return JsonResponse({'code':0,'errmsg':'获取活动列表成功','act_info':act_info})

class Act_detail(View):
    def get(self,request):
        id=request.GET.get('id')
        info=BallTogether.objects.get(id=id)
        return JsonResponse({'code':0,'errmsg':'获取活动详情页信息成功','name':info.name,'site':info.site,
                             'begin_time':info.begin_time,'item':info.EVENT_CHOICE[info.event-1][1],'sign_num':info.sign_num
                             ,'introduce':info.Introduce,'money':info.money
            })


class Join_Act(LoginRequiredJsonMixin,View):
    def post(self,request):
        user=request.user
        data = json.loads(request.body.decode())
        act_id=int(data.get('id'))
        contact=data.get('contact')
        shili=BallTogether.objects.get(id=act_id)
        count=spc_Activity.objects.filter(active_id=act_id,user=user).count()
        if count==0:
            sign_num=BallTogether.objects.get(id=act_id).sign_num
            max_num=BallTogether.objects.get(id=act_id).max_people
            if sign_num<max_num:
                spc_Activity.objects.create(user=user,active=shili,contact=contact)
                book=BallTogether.objects.get(id=act_id)
                book.sign_num+=1
                book.save()
            else:
                return JsonResponse({'code':400,'errmsg':'报名人数已满'})
            return JsonResponse({'code':0,'errmsg':'报名成功'})
        else:
            return JsonResponse({'code':400,'errmsg':'你已经报名过，请勿重复报名'})


class ActPeople(LoginRequiredJsonMixin,View):
    def get(self,request):
        user=request.user
        act_id = request.GET.get('id')
        item_info=spc_Activity.objects.filter(active=act_id)
        count=BallTogether.objects.filter(id=act_id,holder=user).count()
        info=[]
        for item in item_info:
            info.append({
                'img_header':str(item.user.img_header),
                'sex':item.user.sex_gender,
                'nickname':item.user.nickname,
                'contact':item.contact
            })
        return JsonResponse({'code':0,'errmsg':'获取人员数据成功','is_zuzhi':count,'info':info})